package com.bianguo.android.functiondemo.view.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import com.bianguo.android.functiondemo.utils.MLog;
import com.bianguo.android.functiondemo.utils.ScreenUtil;

@SuppressLint("AppCompatCustomView")
public class DrawView extends TextView {

    private Context mContext;
    private int mWidth, mHeight;
    private int screenWidth, screenHeight;
    private float mPreX, mPreY;
    private int left, top, right, bottom;
    private boolean isDraw;

    public DrawView(Context context) {
        super(context);
        initView(context, null);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, @Nullable AttributeSet attrs) {
        this.mContext = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        screenWidth = ScreenUtil.getScreenWidth(mContext);
        screenHeight = ScreenUtil.getScreenHeight(mContext) - getStatusBarHeight();
    }

    private int getStatusBarHeight() {
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        return getResources().getDimensionPixelSize(resourceId);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isDraw = false;
                mPreX = event.getX();
                mPreY = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                isDraw = true;
                final float distanceX = event.getX() - mPreX;
                final float distanceY = event.getY() - mPreY;
                if (Math.abs(distanceX) > 10 || Math.abs(distanceY) > 10) {
                    left = (int) (getLeft() + distanceX);
                    right = left + mWidth;
                    top = (int) (getTop() + distanceY);
                    bottom = top + mHeight;
                    if (left < 0) {
                        left = 0;
                        right = left + mWidth;
                    } else if (right > screenWidth) {
                        right = screenWidth;
                        left = right - mWidth;
                    }
                    if (top < 0) {
                        top = 0;
                        bottom = top + mHeight;
                    } else if (bottom > screenHeight) {
                        bottom = screenHeight;
                        top = bottom - mHeight;
                    }
                    this.layout(left, top, right, bottom);
                }
                break;
            case MotionEvent.ACTION_UP:
                float x = event.getRawX();
                if (x < (screenWidth / 2)) {
                    left = 0;
                    right = left + mWidth;
                    this.layout(left, top, right, bottom);
                } else {
                    right = screenWidth;
                    left = right - mWidth;
                    this.layout(left, top, right, bottom);
                }
                setPressed(false);
                break;
            case MotionEvent.ACTION_CANCEL:
                setPressed(false);
                break;
        }
        MLog.mLog("isDraw = " + isDraw);
        boolean b = isDraw ? isDraw : super.onTouchEvent(event);
        return b;
    }
}
